package com.example.teatea.controller;

import com.example.teatea.dao.AddGood;
import com.example.teatea.dao.CheckGood;
import com.example.teatea.project.Good;
import com.example.teatea.tools.FileTools;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;

public class AddGoodController implements Initializable {
    public TextField name;
    public Button sureBt;
    public Button cancleBt;
    public Button photoBt;
    public ComboBox typeBox;
    public TextField number;
    public String picFileName = "热茶.png";
    public TextField price;
    public ImageView photo;

    public void toSure(ActionEvent actionEvent) {
        Good good = new Good();
        good.setgName(name.getText());
        good.setgNumber(Integer.parseInt(number.getText()));
        good.setgPrice(Float.parseFloat(price.getText()));
        good.setgType(typeBox.getSelectionModel().getSelectedItem().toString());
        good.setgImage(picFileName);
        int r = new AddGood().addGood(good);
        if(r > 0){
            Alert alert = new Alert(Alert.AlertType.INFORMATION);
            alert.setHeaderText("数据添加成功！");
            alert.show();
            Stage stage=(Stage)sureBt.getScene().getWindow();//获取当前窗口
            stage.close();//关闭当前窗口
            MainController mainController = (MainController) ControllerManager.controllerMap.get("main");
            mainController.loadGoodDate();
        }
    }

    public void toCancle(ActionEvent actionEvent) {
        Stage stage = (Stage) cancleBt.getScene().getWindow();
        stage.close();
    }

    public void toPhoto(ActionEvent actionEvent) throws Exception {
        FileChooser fs = new FileChooser();//实例化文件选择器
        fs.setTitle("选择头像");//设置文件选择器标题
        fs.getExtensionFilters().addAll(//设置文件过滤扩展名
                new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                new FileChooser.ExtensionFilter("GIF", "*.gif"),
                new FileChooser.ExtensionFilter("PNG", "*.png")
        );
        Stage stage = (Stage) photo.getScene().getWindow();//获取当前窗体
        File source = fs.showOpenDialog(stage);//打开文件选择器，返回选择的文件
        if (source != null) {
            File dest = FileTools.copyFile(source);//复制头像图片文件到项目指定目录
            Image image = new Image(new FileInputStream(dest));//实例化图片对象
            picFileName = dest.getName();//获取文件名
            photo.setImage(image);//显示图片
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        typeBox.getItems().add("茶");
        typeBox.getItems().add("咖啡");
        typeBox.getItems().add("便餐");
        typeBox.getItems().add("娱乐项目");
        name.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                if(aBoolean && !t1){
                    if(new CheckGood().checkGood(name.getText())){
                        Alert alert=new Alert(Alert.AlertType.INFORMATION);
                        alert.setHeaderText("该商品已存在，请重新输入！");
                        alert.show();
                        name.requestFocus();
                    }
                }
            }
        });
    }
}
